<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		<form id="form">
			<label>书籍名称：</label><input type="text" placeholder="请输入书籍名" name="name" id="name" /><br />
			<label>价 &nbsp;&nbsp;&nbsp;&nbsp; 格：</label><input type="text" placeholder="请输入书籍价格" name="price" id="price" /><br />
			<label>出版日期：</label><input type="date" placeholder="请输入出版日期" name="tdate" id="tdate" /><br />
			<input type="hidden" name="count" />
			&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<button type="button" onclick="add()">保存</button><br />
		</form>
		<script>
			
				let name=document.getElementsByName("name")[0];
				console.log(name);
				let tdate=document.getElementsByName("tdate")[0];
				let price=document.getElementsByName("price")[0];
				let count= document.getElementsByName("count")[0];
				let item=JSON.parse(sessionStorage.getItem("item"));
				if (item) {
					name.value=item.name;
					tdate.value=item.tdate;
					price.value=item.price;
					count.value=item.count;
				}
			
			function add(){
				name=name.value;
				tdate=tdate.value;
				price=price.value;
				let obj={
					id:1,
					name:name,
					tdate:tdate,
					price:price,
					count:count.value?count.value:1
				}
				let data=sessionStorage.getItem("data");
				if(data){
					data=JSON.parse(data);
					if (item) {
						let index=sessionStorage.getItem("index");
						data.splice(index,1,obj);
					} else{
						
					data.push(obj);
					}
					sessionStorage.setItem("data",JSON.stringify(data));
					window.location.href="作业.html";
				}
			}
		</script>
	</body>
</html>
